//
//  WZAcceptController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/7/5.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZAcceptController.h"
#import "WZDetailTask.h"
#import "CFolderMgr.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "CAccountModel.h"
#import "WZRefuseViewController.h"
#import "WZTaskLogController.h"

@interface WZAcceptController ()

@property (weak, nonatomic) IBOutlet UILabel *taskName;
@property (weak, nonatomic) IBOutlet UILabel *desc;
@property (weak, nonatomic) IBOutlet UILabel *beginTime;
@property (weak, nonatomic) IBOutlet UILabel *endTime;
@property (weak, nonatomic) IBOutlet UILabel *projectName;
@property (weak, nonatomic) IBOutlet UILabel *statusName;
@property (weak, nonatomic) IBOutlet UILabel *progress;
@property (weak, nonatomic) IBOutlet UILabel *exturoName;
@property (weak, nonatomic) IBOutlet UILabel *assignerName;

@property (nonatomic, strong) CContactModel *curAccount;
@property (nonatomic, strong) CAccountModel *acc;
@property (nonatomic, strong) WZDetailTask *detailTask;

@property (nonatomic, weak) UIButton *acceptBtn;
@property (nonatomic, weak) UIButton *refuseBtn;

@end

@implementation WZAcceptController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self getTaskDetailInfor];
    
}


- (void)setupTableView {
    
    self.tableView.backgroundColor = WZRGBColor(245, 246, 249);
    self.title = @"任务详情";
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.tableView.tableFooterView.backgroundColor = [UIColor greenColor];
    
    UIButton *acceptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.acceptBtn = acceptBtn;
    [acceptBtn setTitle:@"接受" forState:UIControlStateNormal];
    acceptBtn.backgroundColor = WZRGBColor(94, 196, 206);
    acceptBtn.frame = CGRectMake(0, HARDWARE_SCREEN_HEIGHT - 44 - 64, HARDWARE_SCREEN_WIDTH * 0.5, 44);
    acceptBtn.tag = 100001;
    [acceptBtn addTarget:self action:@selector(acceptAndrefuseBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:acceptBtn];
    
    UIButton *refuseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.refuseBtn = refuseBtn;
    [refuseBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    refuseBtn.backgroundColor = WZRGBColor(247, 187, 66);
    refuseBtn.frame = CGRectMake(CGRectGetMaxX(acceptBtn.frame), HARDWARE_SCREEN_HEIGHT - 44 -64, HARDWARE_SCREEN_WIDTH * 0.5, 44);
    refuseBtn.tag = 100002;
    [refuseBtn addTarget:self action:@selector(acceptAndrefuseBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:refuseBtn];

//    CAccountModel *acc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
//    self.acc = acc;
//    self.curAccount = [[[CContactMgr alloc] init] loadContactWithUserCode:acc.userName];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.scrollEnabled = NO;
}


/**任务详细信息*/
- (void)getTaskDetailInfor {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"name"] = self.oaUserName;
    params[@"pwd"] = self.oaPwd;
    params[@"taskId"] = self.taskId;
    
    [NetWorkShare getTaskDetailInforWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                self.detailTask = [WZDetailTask objectWithKeyValues:responseObject[@"detail"]];
                
                [self loadData];
                
                // 知会我的
                if (![self.oaUserId isEqualToString:self.detailTask.executorId] && ![self.oaUserId isEqualToString:self.detailTask.assignerId]) {
                    
                    self.acceptBtn.hidden = YES;
                    self.refuseBtn.hidden = YES;
                    
                    // 我是分配人不是执行者
                } else if ([self.oaUserId isEqualToString:self.detailTask.assignerId] && ![self.oaUserId isEqualToString:self.detailTask.executorId]) {
                    
                    self.acceptBtn.hidden = YES;
                    self.refuseBtn.hidden = YES;
                    
                    // 我是执行者不是分配者
                } else if (![self.oaUserId isEqualToString:self.detailTask.assignerId] && [self.oaUserId isEqualToString:self.detailTask.executorId]) {
                    
                    self.acceptBtn.hidden = NO;
                    self.refuseBtn.hidden = NO;
                    
                    // 执行和分配是同一个人
                } else if ([self.oaUserId isEqualToString:self.detailTask.assignerId] && [self.oaUserId isEqualToString:self.detailTask.executorId]) {
                    
                    self.acceptBtn.hidden = NO;
                    self.refuseBtn.hidden = NO;
                }
                
                
                [self.tableView reloadData];
                
                WZLog(@"%@", responseObject);
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
        } else {
            
            [MBProgressHUD showError:UNKNOWNERROR];
        }
    } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:NETWORKNERROR];
        WZLog(@"error = %@", error);
    }];
    
}

- (void)loadData {
    
    self.taskName.text = self.detailTask.taskName;
    self.desc.text = self.detailTask.remark;
    self.beginTime.text = self.detailTask.planExecBeginDate;
    self.endTime.text = self.detailTask.planExecEndDate;
    self.projectName.text = self.detailTask.projectName;
    self.exturoName.text = self.detailTask.executorName;
    self.assignerName.text = self.detailTask.assignerName;
    self.statusName.text = self.detailTask.statusName;
    self.progress.text = [NSString stringWithFormat:@"%@%%", self.detailTask.progress];
    
}
- (void)acceptAndrefuseBtnDidClick:(UIButton *)sender {
    
    if (100001 == sender.tag) {
       
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"name"] = self.oaUserName;
        params[@"pwd"] = self.oaPwd;
        params[@"taskId"] = self.taskId;
        params[@"result"] = @"1"; //1 接受 2 拒绝
        params[@"refuseReason"] = @"";
        
        [NetWorkShare acceptAndRefuseWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (responseObject) {
                
                if ([responseObject objectForKey:@"success"]) {
                    WZLog(@"result = %@", responseObject);
                    
                    [MBProgressHUD showSuccess:@"接受任务成功!"];
                    
                } else {
                    [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
                }
            } else {
                
                [MBProgressHUD showError:UNKNOWNERROR];
            }
        } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD showError:NETWORKNERROR];
            WZLog(@"error = %@", error);
        }];
        
    } else {
        
        WZRefuseViewController *refuseVC = [[WZRefuseViewController alloc] init];
        refuseVC.oaUserId = self.oaUserId;
        refuseVC.oaUserName = self.oaUserName;
        refuseVC.taskId = self.taskId;
        refuseVC.oaPwd = self.oaPwd;
        refuseVC.oaCompanyID = self.oaCompanyID;
        refuseVC.assignerId = self.assignerId;
        refuseVC.executorId = self.executorId;
        
        [self.navigationController pushViewController:refuseVC animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (6 == indexPath.row) {
        
        WZTaskLogController *logVc = [[WZTaskLogController alloc] init];
        logVc.taskId = self.taskId;
        logVc.oaUserName = self.oaUserName;
        logVc.oaPwd = self.oaPwd;
        [self.navigationController pushViewController:logVc animated:YES];
    }
}

@end
